package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.device.pojo.MesOilCategory;
import cn.darkhorse.device.pojo.MesOilConsumeInfo;
import cn.darkhorse.device.service.MesOilCategoryService;
import cn.darkhorse.device.service.MesOilConsumeInfoService;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 油耗记录 前端控制器
 * </p>
 *
 * @author yangtianqi
 * @since 2024-11-06
 */
@RestController
@RequestMapping("/device/mesOilConsumeInfo")
@Api(tags = "油耗记录管理")
public class MesOilConsumeInfoController extends BaseController {
    @Autowired
    private MesOilConsumeInfoService mesOilConsumeInfoService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ISysUserService sysUserService;
    @GetMapping("/list")
    @ApiOperation("列表")
    public TableDataInfo list(MesOilConsumeInfo mesOilConsumeInfo){
        startPage();
        List<MesOilConsumeInfo> infoList = mesOilConsumeInfoService.list(mesOilConsumeInfo);
        infoList.forEach(item->{
            if (item.getCreateBy()!=null){
                item.setRegistrant(sysUserService.selectUserByUserName(item.getCreateBy()).getNickName());
            }
        });
        return getDataTable(infoList);
    }

    @GetMapping("/delete")
    @ApiOperation("删除")
    public AjaxResult delete(Long id){
        if (mesOilConsumeInfoService.delete(id)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @PostMapping("/save")
    @ApiOperation("保存")
    public AjaxResult saveConsumeInfo(@RequestBody MesOilConsumeInfo mesOilConsumeInfo){

        if (mesOilConsumeInfoService.saveConsumeInfo(mesOilConsumeInfo)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping("/view")
    @ApiOperation("详情")
    public AjaxResult view(Long id){
        MesOilConsumeInfo consumeInfo = mesOilConsumeInfoService.view(id);
        return AjaxResult.success(consumeInfo);
    }

    @ApiOperation(value = "获取编码")
    @RequestMapping("/getCode")
    public AjaxResult getCode() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",System.currentTimeMillis());
        return AjaxResult.success(jsonObject);
    }

}

